package com.example.wechat.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wechat.login.dto.feedback.FeedbackRequest;
import com.example.wechat.login.dto.feedback.FeedbackResponse;
import com.example.wechat.login.entity.Feedback;
import com.example.wechat.login.mapper.FeedbackMapper;
import com.example.wechat.login.service.FeedbackService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.stream.Collectors;

/**
 * 问题反馈服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    private final FeedbackMapper feedbackMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FeedbackResponse createFeedback(FeedbackRequest request, String unionId) {
        log.info("创建问题反馈，unionId: {}, request: {}", unionId, request);

        // 构建反馈实体
        Feedback feedback = Feedback.builder()
                .unionId(unionId)
                .feedbackType(request.getFeedbackType())
                .content(request.getContent())
                .images(request.getImages())
                .contactInfo(request.getContactInfo())
                .status(0) // 初始状态：未处理
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();

        // 保存到数据库
        save(feedback);

        log.info("问题反馈创建成功，id: {}", feedback.getId());
        return FeedbackResponse.fromEntity(feedback);
    }

    @Override
    public FeedbackResponse getFeedbackById(Long id) {
        log.info("根据ID查询反馈，id: {}", id);
        Feedback feedback = getById(id);
        return FeedbackResponse.fromEntity(feedback);
    }

    @Override
    public Page<FeedbackResponse> getFeedbackPageByUnionId(String unionId, long current, long size) {
        log.info("根据用户unionId分页查询反馈列表，unionId: {}, current: {}, size: {}", unionId, current, size);

        Page<Feedback> page = new Page<>(current, size);
        Page<Feedback> feedbackPage = feedbackMapper.selectPageByUnionId(page, unionId);

        // 转换为响应DTO
        Page<FeedbackResponse> responsePage = new Page<>(
                feedbackPage.getCurrent(),
                feedbackPage.getSize(),
                feedbackPage.getTotal(),
                feedbackPage.isSearchCount()
        );

        responsePage.setRecords(feedbackPage.getRecords().stream()
                .map(FeedbackResponse::fromEntity)
                .collect(Collectors.toList()));

        return responsePage;
    }

    @Override
    public Page<FeedbackResponse> getFeedbackPageByFeedbackType(String feedbackType, long current, long size) {
        log.info("根据反馈类型分页查询反馈列表，feedbackType: {}, current: {}, size: {}", feedbackType, current, size);

        Page<Feedback> page = new Page<>(current, size);
        Page<Feedback> feedbackPage = feedbackMapper.selectPageByFeedbackType(page, feedbackType);

        // 转换为响应DTO
        Page<FeedbackResponse> responsePage = new Page<>(
                feedbackPage.getCurrent(),
                feedbackPage.getSize(),
                feedbackPage.getTotal(),
                feedbackPage.isSearchCount()
        );

        responsePage.setRecords(feedbackPage.getRecords().stream()
                .map(FeedbackResponse::fromEntity)
                .collect(Collectors.toList()));

        return responsePage;
    }

    @Override
    public Page<FeedbackResponse> getFeedbackPageByStatus(Integer status, long current, long size) {
        log.info("根据状态分页查询反馈列表，status: {}, current: {}, size: {}", status, current, size);

        Page<Feedback> page = new Page<>(current, size);
        Page<Feedback> feedbackPage = feedbackMapper.selectPageByStatus(page, status);

        // 转换为响应DTO
        Page<FeedbackResponse> responsePage = new Page<>(
                feedbackPage.getCurrent(),
                feedbackPage.getSize(),
                feedbackPage.getTotal(),
                feedbackPage.isSearchCount()
        );

        responsePage.setRecords(feedbackPage.getRecords().stream()
                .map(FeedbackResponse::fromEntity)
                .collect(Collectors.toList()));

        return responsePage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateFeedbackStatus(Long id, Integer status, String result) {
        log.info("更新反馈状态，id: {}, status: {}, result: {}", id, status, result);

        LambdaUpdateWrapper<Feedback> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Feedback::getId, id)
                .set(Feedback::getStatus, status)
                .set(result != null, Feedback::getResult, result)
                .set(Feedback::getUpdateTime, LocalDateTime.now());

        return update(updateWrapper);
    }
}
